Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Networking → InetAddress Class

Networking

InetAddress Class

The `InetAddress` class in Java represents an Internet Protocol (IP) address. It provides methods to obtain IP addresses, resolve hostnames to IP addresses, and perform other network-related operations. Let's delve into its functionalities with detailed explanations and examples.

InetAddress Methods and Functionality

1. `getByName(String host)`: This is arguably the most frequently used method. It takes a hostname (e.g., "www.google.com") or a dotted-decimal IP address (e.g., "172.217.160.142") as a string and returns an `InetAddress` object representing that address. This method performs a DNS lookup if a hostname is provided.
getByName(String host) import java.net.InetAddress; import java.net.UnknownHostException; public class InetAddressExample { public static void main(String[] args) { try { // Resolve a hostname InetAddress googleAddress = InetAddress.getByName("www.tutorialix.com"); System.out.println("Google's IP address: " + googleAddress.getHostAddress()); // Use a dotted-decimal IP address directly InetAddress ipAddress = InetAddress.getByName("192.168.1.1"); System.out.println("IP Address: " + ipAddress.getHostAddress()); } catch (UnknownHostException e) { System.err.println("Could not resolve hostname or IP address: " + e.getMessage()); } } }
2. `getAllByName(String host)`: Similar to `getByName()`, but returns an array of `InetAddress` objects. This is crucial because a hostname can resolve to multiple IP addresses (e.g., due to load balancing or multiple network interfaces).
getAllByName(String host) import java.net.InetAddress; import java.net.UnknownHostException; public class InetAddressExample2 { public static void main(String[] args) { try { InetAddress[] addresses = InetAddress.getAllByName("www.google.com"); System.out.println("Google's IP addresses:"); for (InetAddress address : addresses) { System.out.println(address.getHostAddress()); } } catch (UnknownHostException e) { System.err.println("Could not resolve hostname: " + e.getMessage()); } } }
3. `getHostAddress()`: Returns the IP address of the `InetAddress` object as a string in dotted-decimal notation (e.g., "192.168.1.1"). 4. `getHostName()`: Returns the hostname associated with the `InetAddress` object. If the `InetAddress` was created from a dotted-decimal IP address, it might perform a reverse DNS lookup to try and obtain the hostname. 5. `isReachable(int timeout)`: Checks if a host is reachable within a specified timeout (in milliseconds). This method doesn't guarantee connectivity but attempts to send a packet to the host and checks for a response.
isReachable(int timeout) import java.net.InetAddress; import java.net.UnknownHostException; import java.io.IOException; public class InetAddressExample3 { public static void main(String[] args) { try { InetAddress host = InetAddress.getByName("www.tutorialix.com"); boolean reachable = host.isReachable(5000); // Timeout of 5 seconds System.out.println("Host reachable: " + reachable); } catch (UnknownHostException | IOException e) { System.err.println("Error checking reachability: " + e.getMessage()); } } }
6. `getAddress()`: Returns the raw IP address as a byte array. This is useful for low-level network programming. 7. `getLocalHost()`: Returns the `InetAddress` object representing the local host (the machine running the Java code). Important Considerations: `UnknownHostException`: This exception is thrown by `getByName()` and `getAllByName()` if the hostname or IP address cannot be resolved. Always handle this exception using a `try-catch` block. `IOException`: The `isReachable()` method can throw an `IOException` if there's a problem during network communication. Network Connectivity: The success of methods like `isReachable()` depends on network connectivity and firewall settings. DNS Resolution: The time taken for DNS resolution can vary. Consider using asynchronous methods for better performance in applications where responsiveness is critical.

Tutorials